﻿using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;
using MonkeyShop.Web.Biz;
using MonkeyShop.Web.Models;

namespace MonkeyShop.Web
{
    public partial class Results : System.Web.UI.Page
    {
        private List<ProductDetailsModel> _productDetailsModels;
        protected List<ProductDetailsModel> ProductDetailsModels
        {
            get
            {
                if (_productDetailsModels == null)
                {
                    // check session first
                    var desktopModel = WebCommon.CatalogCommon.SavedDesktopModel;
                    if (desktopModel == null)
                    {
                        // chek cookie
                        var desktopModelCookie = Request.Cookies["desktop-model"];
                        if (desktopModelCookie != null)
                        {
                            var serializer = new JavaScriptSerializer();
                            try
                            {
                                desktopModel = serializer.Deserialize<DesktopModel>(Server.UrlDecode(desktopModelCookie.Value));
                                WebCommon.CatalogCommon.SavedDesktopModel = desktopModel;
                            }
                            catch (Exception ex)
                            {
                                
                            }
                        }
                    }
                    _productDetailsModels = DesktopBuilderBiz.GetDesktopProducts(desktopModel);
                }
                //return _productDetailsModels ??
                //       (_productDetailsModels =
                //           DesktopBuilderBiz.GetDesktopProducts(WebCommon.CatalogCommon.SavedDesktopModel));
                return _productDetailsModels;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            var desktopModel = WebCommon.CatalogCommon.SavedDesktopModel;
            Literal1.Text = desktopModel != null ? string.Join(",", desktopModel.ProductIds) : "null";
        }
    }
}